﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using E2_WEBSITE;
using System.Collections;
using System.Text;

public partial class cp_member_mycart : OboutInc.oboutAJAXPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int iDaLogin = (Int32)Session["IsLogin"];
        if (iDaLogin == 0)
        {
            Response.Redirect("/Common/Login.aspx");
            return;
        }

        if (!IsPostBack)
        {
            DataBindingListBasket();
        }
        
        if ((Session["CurrentBasketLoaded"] != null && (bool)Session["CurrentBasketLoaded"]))
        {
            Session["CurrentBasketLoaded"] = false;

            int bID = 1;
            if (IsCallback && Session["CurrentBasketID"] != null)
            {
                bID = Int32.Parse(Session["CurrentBasketID"].ToString());
                LoadBasketDetails(bID);
                setCurrentBasketID(bID);
            }
        }
        
        if ((Session["BasketListLoaded"] != null && (bool)Session["BasketListLoaded"]))
        {
            Session["BasketListLoaded"] = false;

            int memID = 1;
            int bID = 1;
            if (IsCallback && Session["UserID"] != null)
            {
                
                memID = Int32.Parse(Session["UserID"].ToString());
                Basket.GlobalBasket.addNewBasket(memID);
                LoadBasketList(memID);

                Session["CurrentBasketID"] = Basket.GlobalBasket.getCurrentBasketID((Int32)Session["UserID"]);
                if (Session["CurrentBasketID"] != null)
                {
                    bID = Int32.Parse(Session["CurrentBasketID"].ToString());
                    LoadBasketDetails(bID);
                }
            }
        }
        
        if ((Session["UpdatePriceCurrentBasket"] != null && (bool)Session["UpdatePriceCurrentBasket"]))
        {
            Session["UpdatePriceCurrentBasket"] = false;

            int bID = 1;
            int memID = 1;
            if (IsCallback && Session["CurrentBasketID"] != null && Session["UserID"] != null)
            {
                bID = Int32.Parse(Session["CurrentBasketID"].ToString());
                memID = Int32.Parse(Session["UserID"].ToString());
                Basket.GlobalBasket.updatePrice(bID);
                LoadBasketDetails(bID);
                LoadBasketList(memID);
            }
        }
        
        
        if ((Session["UpdateBasketList"] != null && (bool)Session["UpdateBasketList"]))
        {
            Session["UpdateBasketList"] = false;
            if (IsCallback && Session["CurrentBasketID"] != null && Session["UserID"] != null)
            {
                DataBindingListBasket();
            }
        }
    }

    protected void LoadBasketList(int mID)
    {
        gridListBasket.DataSource = Basket.GlobalBasket.getListBasket(mID);
        gridListBasket.DataBind();
    }

    protected void DataBindingListBasket()
    {
        if (Session["UserID"] != null)
        {
            Session["CurrentBasketID"] = Basket.GlobalBasket.getCurrentBasketID((Int32)Session["UserID"]);
            LoadBasketList((Int32)Session["UserID"]);
            if (Session["CurrentBasketID"] != null)
                LoadBasketDetails((Int32)Session["CurrentBasketID"]);
        }
    }

    public void LoadBasketDetail(int bID)
    {
        Session["CurrentBasketID"] = bID;
        Session["CurrentBasketLoaded"] = true;
    }

    public void UpdateBasketList()
    {
        Session["UpdateBasketList"] = true;
    }

    public void UpdatePriceCurrentBasket()
    {
        Session["UpdatePriceCurrentBasket"] = true;
    }

    protected void LoadBasketDetails(int bID)
    {
        gridBasketDetail.DataSource = Basket.GlobalBasket.getBasketDetail(bID);
        gridBasketDetail.DataBind();
    }

    protected void setCurrentBasketID(int bID)
    {
        Basket.GlobalBasket.setCurrentBasketID(bID);
    }

    
    protected string IsChecked(bool bChecked)
    {
        return bChecked ? "checked=\"checked\"" : "";
        
    }

    public void AddNewBasket()
    {
        Session["BasketListLoaded"] = true;
    }
    
    protected bool IsChecked(string sChecked)
    {
        if (sChecked.Trim().CompareTo("True") == 0)
            return true;
        return false;
    }

    protected string GetUpdateStatus(object oStatus)
    {
        if (oStatus.ToString() == "0")
            return "CheckMark.png";
        else
            return "CheckMarkX.png";
    }

    protected void gridListBasket_InsertCommand(object sender, Obout.Grid.GridRecordEventArgs e)
    {

    }

    protected void gridListBasket_DeleteCommand(object sender, Obout.Grid.GridRecordEventArgs e)
    {
        object obj1 = e.Record["MaSoGio"];
        object obj2 = Session["UserID"];
        if (obj1 != null && obj2 != null)
        {
            int bID;
            int mID;
            if (Int32.TryParse(obj1.ToString(), out bID)
                && Int32.TryParse(obj2.ToString(), out mID))
            {
                Basket.GlobalBasket.deleteBasket(bID, mID);
            }
        }
    }

    protected void RebindBasketList(object sender, EventArgs e)
    {
        if (Session["UserID"] != null)
            LoadBasketList((Int32)Session["UserID"]);
    }


    protected void gridBasketDetail_DeleteCommand(object sender, Obout.Grid.GridRecordEventArgs e)
    {
        object obj1 = e.Record["idChiTietGioHang"];
        object obj2 = e.Record["idGioHang"];
        if (obj1 != null && obj2 != null)
        {
            int bID;
            int bdID;
            if (Int32.TryParse(obj1.ToString(), out bdID)
                && Int32.TryParse(obj2.ToString(), out bID))
            {
                Basket.GlobalBasket.removeFromBasket(bID, bdID);
            }
        }
    }

    protected void gridBasketDetail_UpdateCommand(object sender, Obout.Grid.GridRecordEventArgs e)
    {
        object obj1 = e.Record["idChiTietGioHang"];
        object obj2 = e.Record["SoLuong"];
        if (obj1 != null && obj2 != null)
        {
            int bdID;
            int quan;
            if (Int32.TryParse(obj1.ToString(), out bdID)
                && Int32.TryParse(obj2.ToString(), out quan))
            {
                Basket.GlobalBasket.updateProductQuan(bdID, quan);
            }
        }
    }

    protected void gridBasketDetail_Rebind(object sender, EventArgs e)
    {
        if (Session["CurrentBasketID"] != null)
            LoadBasketDetails((Int32)Session["CurrentBasketID"]);
    }

    public int GetMaxQuantity(int imgID)
    {
        return Basket.GlobalBasket.getMaxQuantity(imgID);
    }

    private void runjQueryCode(string jsCodetoRun)
    {

        ScriptManager requestSM = ScriptManager.GetCurrent(this);
        if (requestSM != null && requestSM.IsInAsyncPostBack)
        {
            ScriptManager.RegisterClientScriptBlock(this,
                                                    typeof(Page),
                                                    Guid.NewGuid().ToString(),
                                                    jsCodetoRun,
                                                    true);
        }
        else
        {
            ClientScript.RegisterClientScriptBlock(typeof(Page),
                                                   Guid.NewGuid().ToString(),
                                                   jsCodetoRun,
                                                   true);
        }
    }

    protected void imgPay_Click(object sender, ImageClickEventArgs e)
    {
        if (Session["UserID"] != null)
        {
            Session["CurrentBasketID"] = Basket.GlobalBasket.getCurrentBasketID((Int32)Session["UserID"]);
            if (Session["CurrentBasketID"] != null)
            {
                int bID;
                if (Int32.TryParse(Session["CurrentBasketID"].ToString(), out bID))
                {
                    if (Basket.GlobalBasket.checkBasketEmpty(bID))
                    {
                        //ServerAlert_EmptyBasket.Show();
                        runjQueryCode("alert('Giỏ hàng của bạn rỗng !');");
                        return;
                    }

                    if (!Basket.GlobalBasket.checkAllPriceUpdated(bID))
                    {
                        //ServerAlert_NotUpdatePrice.Show();
                        runjQueryCode("alert('Bạn chưa cập nhật đơn giá hiện tại cho tất cả các sản phẩm !');");
                        return;
                    }
                    if (!Basket.GlobalBasket.checkAppropriateQuantity(bID))
                    {
                        //ServerAlert_NotAppropriateQuantity.Show();
                        runjQueryCode("alert('Một số sản phẩm có số lượng vượt quá số lượng mà công ty có khả năng cung cấp! Xin cập nhật lại số lượng cho những sản phẩm đó.');");
                        return;
                    }

                    runjQueryCode("parent.location.href = " + "'pay.aspx?mID=" + Session["UserID"].ToString() + "&bID=" + bID.ToString() + "'");
                }
            }
        }
    }
}